package util;

import math.Operators;
import model.altarica_fields.Var;

public class BinaryTree {

    private BinaryTree left;
    private BinaryTree right;
    private Object value;

    public BinaryTree(Object value) {

        this.value = value;

        this.left = null;
        this.right = null;
    }

    public BinaryTree() {

        this.value = null;

        this.left = null;
        this.right = null;
    }

    public void setValue(Object value) {

        this.value = value;
    }

    public Object getValue() {

        return this.value;
    }

    public boolean isLeaf() {

        return (left == null && right == null);
    }

    public void addLeft(BinaryTree left) {

        this.left = left;
    }

    public void addRight(BinaryTree right) {

        this.right = right;
    }

    public BinaryTree getLeft() {

        return this.left;
    }

    public BinaryTree getRight() {

        return this.right;
    }

    public void copy(BinaryTree t) {

        this.value = t.getValue();
        this.left = t.getLeft();
        this.right = t.getRight();
    }

    @Override
    public String toString() {

        String str = new String();

        if (left != null) {
            str += left.toString() + " ";
        }
        if (value instanceof Var) {
            str += ((Var) value).getId();
        } else {
            if (value instanceof Operators) {
                switch ((Operators) value) {
                    case AND:
                        str += "and";
                        break;
                    case OR:
                        str += "or";
                        break;
                    case IMPLY:
                        str += "=>";
                        break;
                    case EQUAL:
                        str += "=";
                        break;
                    case NEQUAL:
                        str += "and";
                        break;
                    case LEQ:
                        str += "<=";
                        break;
                    case GEQ:
                        str += ">=";
                        break;
                    case LESS:
                        str += "<";
                        break;
                    case GREATER:
                        str += ">";
                        break;
                    case PLUS:
                        str += "+";
                        break;
                    case MINUS:
                        str += "-";
                        break;
                    case STAR:
                        str += "*";
                        break;
                    case SLASH:
                        str += "/";
                        break;
                    case NOT:
                        str += "not";
                        break;
                    case TRUE:
                        str += "true";
                        break;
                    case FALSE:
                        str += "false";
                        break;
                    case IF:
                        str += "if";
                        break;
                    case THEN:
                        str += "then";
                        break;
                    case ELSE:
                        str += "else";
                        break;
                }
            } else {
                str += value;
            }
        }

        if (right != null) {
            str += " " + right.toString();
        }
        return str;
    }
}
